package com.ivo.biz;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.ivo.quartz.WHPerformanceQuartz;
import com.ivo.util.JsonUtil;


@RestController
@RequestMapping("/wh/performance")
public class WHPerformanceController {
	@Autowired
	private PerformanceService performanceService;
	
	@Autowired
	private WHPerformanceQuartz whPerformanceQuartz;
	
	
	@RequestMapping(value = "/whPerformance")
	@ResponseBody
	public ModelAndView getPerformance(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/wh");
		mv.addObject("category","whPerformance");
		return mv;
		
	}
	
	
	@RequestMapping(value = "/whgroups")
	@ResponseBody
	public ModelAndView whgroups(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("sys/wh");
		mv.addObject("category","whgroups");
		return mv;
		
	}
	
	
	@RequestMapping(value = "/getWHPerformance")
    @ResponseBody
    public String list(
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "bonus", required = false) Double bonus,
    		@RequestParam(value = "score", required = false) Double score,
    		@RequestParam(value = "dept_ID", required = false) String dept_ID) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getWHPerformance(allFlag,pno,emp_id,month,bonus,score,dept_ID));
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	
	
	@RequestMapping(value = "/save")
    @ResponseBody
    public String save(HttpServletRequest request,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "param", required = false) String jsonList) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        List<Map<String, String>> list = JsonUtil.toListMaps(jsonList);
        if(list!=null)
        returnMap.put("result", performanceService.saveWHPerformance(list));
        
        if("0".equals(performanceService.saveWHPerformance(list))){
        	whPerformanceQuartz.CalculateBonus(month);
        }
        
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	@RequestMapping(value = "/getWHBonus")
    @ResponseBody
    public String getBonus(
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "dept_ID", required = false) String dept_ID,
    		@RequestParam(value = "score", required = false) String score,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "bonus", required = false) String bonus
    		) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getWHBonus( month,  dept_ID, emp_id, score, bonus));
        return JsonUtil.toJson(returnMap);
    }
	
	
	
	@RequestMapping(value = "/getwhgroups")
    @ResponseBody
    public String getwhgroups(
    		@RequestParam(value = "allFlag", required = false) int allFlag,
    		@RequestParam(value = "pno", required = false) int pno,
    		@RequestParam(value = "emp_id", required = false) String emp_id,
    		@RequestParam(value = "month", required = false) String month,
    		@RequestParam(value = "dept_ID", required = false) String dept_ID) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.getwhgroups(allFlag,pno,emp_id,month,dept_ID));
        return JsonUtil.toJson(returnMap);
    }
	
	
	
    
	
	@RequestMapping(value = "/savegroups")
    @ResponseBody
    public String savewhgroups(HttpServletRequest request,
    		@RequestParam(value = "param", required = false) String jsonList) {
    	Map<String, Object> returnMap = new HashMap<String, Object>();
    	
    	  List<Map<String, String>> list = JsonUtil.toListMaps(jsonList);
		
    	returnMap.put("err_code", "0");
        returnMap.put("err_msg", "success");
        returnMap.put("result", performanceService.savewhgroups(list));
        
        
        
        
        return JsonUtil.toJson(returnMap);
    }
	
	
		
	
}
